import React, { useEffect, useState } from 'react';
import { ISession, TargetType } from '@/ts/core';
import PreviewLayout from '../layout';
import SessionItem from './content';
const SessionBody = ({
  session,
  relation,
  height,
}: {
  height?: string;
  session: ISession;
  relation?: boolean;
}) => {
  const [actions, setActions] = useState<{ key: string; label: string }[]>([]);
  const [bodyType, setBodyType] = useState('');
  useEffect(() => {
    const newActions = [
      {
        key: 'activity',
        label: '动态',
      },
    ];
    if (session.id === session.target.id) {
      newActions.push({
        key: 'store',
        label: '数据',
      });
      if (
        session.target.hasRelationAuth() ||
        session.target.typeName !== TargetType.Storage
      ) {
        newActions.push({
          key: 'relation',
          label: '关系',
        });
        if (relation) {
          setBodyType('relation');
        }
      }
    }
    if (
      session.isMyChat &&
      (session.target.typeName !== TargetType.Storage || session.target.hasRelationAuth())
    ) {
      newActions.unshift({
        key: 'chat',
        label: '沟通',
      });
      const hasHomeInPath = location.href.includes('home');
      if (hasHomeInPath || relation !== true) {
        setBodyType('chat');
      }
    }
    newActions.push(
      {
        key: 'setting',
        label: '设置',
      },
      {
        key: 'smart',
        label: '智能体',
      },
    );
    setBodyType((pre) => (pre === '' ? 'activity' : pre));
    setActions(newActions);
  }, [session]);

  return (
    <PreviewLayout
      height={height}
      entity={session}
      actions={actions}
      selectKey={bodyType}
      onActionChanged={(key) => {
        setBodyType(key as string);
      }}
      number={session.target.memberCount}>
      <SessionItem bodyType={bodyType} session={session} />
    </PreviewLayout>
  );
};
export default SessionBody;
